package ar.edu.unq.wannaplay.pages.abm.employees;

import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.dtos.EmployeeDTO;
import ar.edu.unq.wannaplay.dtos.LicenceDTO;
import ar.edu.unq.wannaplay.pages.EmployeePage;
import ar.edu.unq.wannaplay.pages.abm.WPABM;
import ar.edu.unq.wannaplay.services.EmployeeService;

public class AddLicencePage extends WPABM<EmployeeDTO, EmployeePage> {
    private static final long serialVersionUID = 1L;

    private LicenceDTO licence;

    @SpringBean(name = "service.employeeService")
    private EmployeeService employeeService;

    public AddLicencePage(final PageParameters parameter) {
        super(EmployeePage.class);
        this.setAbmObject(this.getEmployeeService().getEmployeeDTOById(parameter.getString("employee")));
        this.init();
    }

    @Override
    protected void accept() {
        AddLicencePage.this.getEmployeeService().giveLicenceTo(AddLicencePage.this.getAbmObject(),
                AddLicencePage.this.getLicence());
    }

    protected void init() {
    	licence = new LicenceDTO();
        WebMarkupContainer container = new WebMarkupContainer("container");
        this.addBody(container);
        this.add(container);
    }

    private void addBody(final WebMarkupContainer container) {
        container.add(new TextField<LicenceDTO>("name", new PropertyModel<LicenceDTO>(this.getLicence(), "name")));
        TextField<LicenceDTO> licenceStart = new TextField<LicenceDTO>("from", new PropertyModel<LicenceDTO>(
                this.getLicence(), "licenceStart"));
        TextField<LicenceDTO> licenceEnd = new TextField<LicenceDTO>("to", new PropertyModel<LicenceDTO>(
                this.getLicence(), "licenceEnd"));
        licenceStart.add(new DatePicker());
        licenceEnd.add(new DatePicker());
        container.add(licenceStart);
        container.add(licenceEnd);
    }

    // setters&getters
    public LicenceDTO getLicence() {
        return licence;
    }

    public void setLicence(final LicenceDTO licence) {
        this.licence = licence;
    }

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }
}
